package com.todo.biz.application.frontend.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.todo.biz.application.convert.BizBaseConvert;
import com.todo.biz.application.entity.bo.CommentBO;
import com.todo.biz.application.entity.condition.CommentConditionBO;
import com.todo.biz.application.frontend.service.CommentAggService;
import com.todo.biz.application.remote.UserInfoRemoteService;
import com.todo.biz.application.service.CommentService;
import com.todo.biz.common.enums.CommentTypeEnum;
import com.todo.biz.iface.frontend.request.InsertCommendParam;
import com.todo.biz.iface.frontend.request.QueryCommentByParentIdParam;
import com.todo.biz.iface.frontend.request.RemoveCommentByIdParam;
import com.todo.biz.iface.frontend.response.CommentVO;
import com.todo.common.common.entity.response.BizPageResponse;
import com.todo.common.common.entity.response.BizResponse;
import com.todo.common.common.entity.response.page.PageResult;
import com.todo.common.common.enums.BizErrorCodeEnum;
import com.todo.common.common.exception.BizException;
import com.todo.user.api.response.BatchQuerySimplyUserInfoResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;

/**
 * (Comment)表服务实现类
 *
 * @author zhangwenxiang
 * @since 2024-03-04 00:19:45
 */
@Slf4j
@Service("commentAggService")
public class CommentAggServiceImpl implements CommentAggService {

    @Resource
    private CommentService commentService;
    @Resource
    private UserInfoRemoteService userInfoRemoteService;

    @Override
    public BizResponse<Boolean> insertComment(InsertCommendParam param) {
        if (param == null || !param.baseCheck()) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }

        // TODO 内容基础审核即可，审核通过则数据保存
        CommentBO commentBO = BizBaseConvert.convertToCommentBO(param);
        boolean insertResult = commentService.insertComment(commentBO);
        if (insertResult) {
            //TODO 评论完成后提示消息 1、被评论人 2、作品作者
        }
        return BizResponse.success(insertResult);
    }

    @Override
    public BizResponse<Boolean> removeCommentById(RemoveCommentByIdParam param) {
        if (param == null || !param.baseCheck()) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        CommentBO commentBO = commentService.queryCommentById(Long.valueOf(param.getCommentId()));
        if (commentBO == null) {
            log.info("CommentAggService#removeCommentById 删除评论完成，根据主键查询数据为空，默认删除完成 param:【{}】", JSONObject.toJSONString(param));
            return BizResponse.success(true);
        }
        if (!commentBO.getCommentUser().equals(param.get_user().getUserId())) {
            throw new BizException(BizErrorCodeEnum.ILLEGAL_OPERATION, "无权删除非自己的评论");
        }
        Boolean removeResult = commentService.removeCommentById(Long.valueOf(param.getCommentId()));
        return BizResponse.success(removeResult);
    }

    @Override
    public BizPageResponse<CommentVO> queryCommentByParentId(QueryCommentByParentIdParam param) {
        if (param == null || !param.baseCheck()) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        CommentConditionBO request = new CommentConditionBO();
        request.setPageIndex(param.getPageIndex());
        request.setPageSize(param.getPageSize());
        request.setPageIndex(param.getPageIndex());
        request.setParentId(Long.valueOf(param.getParentId()));
        request.setCommentType(CommentTypeEnum.getByType(param.getCommentType()));
        PageResult<CommentBO> pageResult = commentService.queryCommentByCondition(request);
        if (pageResult == null) {
            log.info("CommentAggService#queryCommentByParentId 根据父id查询评论数据为空，默认返回空数据 param:【{}】", JSONObject.toJSONString(param));
            return BizPageResponse.success();
        }
        if (CollectionUtils.isEmpty(pageResult.getResults())) {
            return BizPageResponse.success();
        }
        Set<Long> userIdSet = new HashSet<>();
        for (CommentBO commentBO : pageResult.getResults()) {
            if (commentBO != null) {
                userIdSet.add(commentBO.getBeRepliedUser());
                userIdSet.add(commentBO.getCommentUser());
            }
        }
        BizResponse<BatchQuerySimplyUserInfoResponse> response = userInfoRemoteService.batchQuerySimplyUserInfo(new ArrayList<>(userIdSet));
        if (response == null || !response.isSuccess() || response.getData() == null || MapUtils.isEmpty(response.getData().getSimplyUserInfoMap())) {
            log.warn("CommentAggService#queryCommentByParentId 根据用户Id查询详情数据为空 默认返回空 userIdList :【{}】", userIdSet);
            return BizPageResponse.success();
        }
        return BizPageResponse.success(BizBaseConvert.convertToCommentVOPageResult(pageResult, response.getData().getSimplyUserInfoMap()));
    }
}
